package model

import (
	"github.com/BurntSushi/toml"
	"log"
	"time"
)

const (
	SUBSCRIBE_CDS = "CDS"
	SUBSCRIBE_LDS = "LDS"
	SUBSCRIBE_RDS = "RDS"
	SUBSCRIBE_ADS = "ADS"
	SUBSCRIBE_EDS = "EDS"




	ENVOY_SUBSCRIBER_KEY = "envoySubscriber"
	VERSION = "V0"
)

// Not really constants
var SUPPORTED_TYPES = []string{SUBSCRIBE_CDS, SUBSCRIBE_LDS, SUBSCRIBE_RDS, SUBSCRIBE_EDS}
var ENV_PATH = "/.env"
var CONSUL_PREFIX = "xDS"
var FILE_MODE = false
var FOLDER_PATH = ""
var POLL_INTERVAL = 10 * time.Second



type ConfigMeta struct {
	Key     string
	Topic   string
	Version string
}



type RouteConfig struct {
	Name string
	Routes []*Route
}

func (rc *RouteConfig) Set(s string) error {
	log.Printf("Set RouteConfig %s\n", s)
	return toml.Unmarshal([]byte(s), rc)
}

type Route struct {
	Match string
	Cluster string
}

type ClusterConfig struct {
	Clusters []*Cluster
}

type Cluster struct {
	Name string
	ConnectTimeout int64
	Address string
	Port uint32
}

func (cc *ClusterConfig) Set(s string) error {
	log.Printf("Set ClusterConfig: ")
	err := toml.Unmarshal([]byte(s), cc)
	for _, c := range cc.Clusters {
		log.Println(c)
	}
	return err
}